package com.xuehu.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xuehu.cloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author guoweia@cslc.com.cn
 * @createTime 2020/12/2 10:51
 * @description 调用OrderHystrixController时，需清理OrderController的相关配置
 */
@Slf4j
@RestController
@DefaultProperties(defaultFallback = "globalOrderFallback")
public class OrderController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/order/pay/hystrix/ok/{id}")
    public String paymentOk(@PathVariable("id") Long id){
        String result = paymentFeignService.paymentOk(id);
        log.info("***paymentOk-result:" + result);
        return result;
    }

    @HystrixCommand
    @GetMapping("/order/hystrix/global")
    public String paymentGlobal(){
        int i = 10/0;
        return "线程池：" + Thread.currentThread().getName() + ",paymentGlobal成功!";
    }

//    @HystrixCommand(fallbackMethod = "orderTimeoutFallback"
//            ,commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")}
//    )
    @GetMapping("/order/pay/hystrix/timeout/{id}")
    public String paymentTimeout(@PathVariable("id") Long id){
        String result = paymentFeignService.paymentTimeout(id);
        log.info("***paymentTimeout-result:" + result);
        return result;
    }

    public String orderTimeoutFallback(Long id) {
        return "线程池：" + Thread.currentThread().getName() + ",系统繁忙,orderTimeoutFallback成功，id=" + id;
    }

    public String globalOrderFallback() {
        return "线程池：" + Thread.currentThread().getName() + ",系统繁忙,globalOrderFallback成功!";
    }
}
